//
//  EditAddressViewController.m
//  LoveFreshBee
//
//  Created by brice Mac on 2016/10/19.
//  Copyright © 2016年 王忠迪. All rights reserved.
//

#import "EditAddressViewController.h"
#import "ZHMyAddressData.h"
#import "SVProgressHUD.h"

static NSString* cellId = @"EditAddressCell1";

@interface EditAddressViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,weak)UIButton* lastBtn;

@property(nonatomic,weak)UITextField* textFiled0;
@property(nonatomic,weak)UITextField* textFiled2;
@property(nonatomic,weak)UITextField* textFiled3;
@property(nonatomic,weak)UITextField* textFiled4;
@property(nonatomic,weak)UITextField* textFiled5;


@end

@implementation EditAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.textFiled0 becomeFirstResponder];
    
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 10;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
    
    self.navigationItem.title = @"修改地址";
    
    UIBarButtonItem* saveItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(save)];
    saveItem.tintColor = [UIColor blackColor];
    self.navItem.rightBarButtonItem = saveItem;
    
    [self setupHeaderAndFooterView];
}

- (void)setupTableView {
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
}

//设置头部和尾部
- (void)setupHeaderAndFooterView {
    
    //设置tableView的头部灰色View
    UIView *headerBgColor = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 15)];
    self.tableView.tableHeaderView = headerBgColor;
    
    //设置tableView的尾部灰色View
    UIView *footerBgColor = [[UIView alloc]initWithFrame:self.tableView.bounds];
    self.tableView.tableFooterView = footerBgColor;
}

    
-(void)save{
    
    self.model.accept_name = self.textFiled0.text;
    //字符串转成nsnuber;
    self.model.telphone = self.textFiled2.text;
    self.model.city_name = self.textFiled3.text;
    self.model.district_name = self.textFiled4.text;
    self.model.address = self.textFiled5.text;
    
    //判断逻辑
    if (self.model.accept_name.length ==0 ) {
        
        [SVProgressHUD setBackgroundColor:[UIColor darkGrayColor]];
        [SVProgressHUD showImage:[[UIImage imageNamed:@"v2_orderSuccess"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] status:@"我们需要你的大名"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
            
            
        });
        
        return;
    }
    
    //默认有一个数字是0
    //法1
    //    if ([NSString stringWithFormat:@"%ld",(long)[self.model.telphone integerValue]].length==0||[NSString stringWithFormat:@"%ld",(long)[self.model.telphone integerValue]].length==1) {
    //法2
    if ([[NSString stringWithFormat:@"%ld",(long)[self.model.telphone integerValue]] isEqualToString:@"0"]) {
        
        [SVProgressHUD showImage:[[UIImage imageNamed:@"v2_orderSuccess"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] status:@"我们需要你的手机号"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
            
            
        });
        return;
    }
    
    if (self.model.address.length ==0 ) {
        
        [SVProgressHUD setBackgroundColor:[UIColor darkGrayColor]];
        [SVProgressHUD showImage:[[UIImage imageNamed:@"v2_orderSuccess"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] status:@"我们需要你的详细地址"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
            
            
        });
        
        return;
    }
    
    if (_callbackClick) {
        _callbackClick(NO);
        
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 6;
    }
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    
    //设置cell选中的样式为不显示
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.section==0) {
        UITextField* textFiled = [[UITextField alloc]init];
      
        
        if(indexPath.row ==0){
            cell.textLabel.text = @"联系人";
            textFiled.text = self.model.accept_name;
            self.textFiled0 = textFiled;
            
        }else if (indexPath.row ==1){
            
            
            UIButton* btn1 = [[UIButton alloc]init];
            [btn1 addTarget:self action:@selector(changeGendenle:) forControlEvents:UIControlEventTouchUpInside];
            [btn1 setImage:[UIImage imageNamed:@"v2_noselected"] forState:UIControlStateNormal];
            [btn1 setImage:[UIImage imageNamed:@"v2_selected"] forState:UIControlStateSelected];
            [btn1 setTitle:@"先生" forState:UIControlStateNormal];
            [btn1 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            [btn1.titleLabel setFont:[UIFont systemFontOfSize:15]];
            [btn1 sizeToFit];
            
            btn1.center = CGPointMake(145, cell.contentView.frame.size.height/2);
            [cell.contentView addSubview:btn1];
            
            
            UIButton* btn2 = [[UIButton alloc]init];
            [btn2 addTarget:self action:@selector(changeGendenle:) forControlEvents:UIControlEventTouchUpInside];
            [btn2 setImage:[UIImage imageNamed:@"v2_noselected"] forState:UIControlStateNormal];
            [btn2 setImage:[UIImage imageNamed:@"v2_selected"] forState:UIControlStateSelected];
            [btn2 setTitle:@"女士" forState:UIControlStateNormal];
            [btn2 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            [btn2.titleLabel setFont:[UIFont systemFontOfSize:15]];
            [btn2 sizeToFit];
            
            btn2.center = CGPointMake(CGRectGetMaxX(btn1.frame)+60, cell.contentView.frame.size.height/2);
            [cell.contentView addSubview:btn2];
            

            
            if([self.model.gender intValue] == 1){
                btn1.selected = YES;
                self.lastBtn = btn1;
            }else{
                btn2.selected = YES;
                self.lastBtn = btn2;
            }
            
            
            
        }else if (indexPath.row ==2){
            
            self.textFiled2 = textFiled;
            cell.textLabel.text = @"手机号码";
            textFiled.keyboardType = UIKeyboardTypePhonePad;
            textFiled.text= [NSString stringWithFormat:@"%@",self.model.telphone];
            
        }else if (indexPath.row ==3){
            self.textFiled3 = textFiled;
            cell.textLabel.text = @"所在城市";
            textFiled.text = self.model.city_name;
            
        }else if (indexPath.row ==4){
            self.textFiled4 = textFiled;
            cell.textLabel.text = @"所在地区";
            textFiled.text = self.model.district_name;
            
        }else if (indexPath.row ==5){
            
            self.textFiled5 = textFiled;
            cell.textLabel.text = @"详细地址";
            textFiled.text = self.model.address;
        }
        
        if (indexPath.row !=1){
            [textFiled sizeToFit];
            textFiled.frame = CGRectMake(120, 0, textFiled.frame.size.width, cell.contentView.frame.size.height);
            
            textFiled.textColor= [UIColor darkGrayColor];
            
            [cell.contentView addSubview:textFiled];
            
        }
        
        cell.textLabel.textColor = [UIColor darkGrayColor];
        
    }else{
        
        
        cell.textLabel.text = @"删除当前地址";
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        //        cell.textLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.center=cell.center;
        
        
        
    }
    
    
    
    
    
    return cell;
}

-(void)changeGendenle:(UIButton*)btn{
    
    self.lastBtn.selected = NO;
    [btn setSelected:YES];
    
    self.lastBtn = btn;
    
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //文字为对应航间距
    if (indexPath.section==0) {
        if(indexPath.row ==0){
            //            cell.textLabel.text = @"联系人";
            
        }else if (indexPath.row ==1){
            //            cell.textLabel.text = @"删除当前地址";
            
        }else if (indexPath.row ==2){
            //            cell.textLabel.text = @"手机号码";
            
        }else if (indexPath.row ==3){
            //            cell.textLabel.text = @"所在城市";
            
        }else if (indexPath.row ==4){
            //            cell.textLabel.text = @"所在地区";
            
        }else if (indexPath.row ==5){
            //            cell.textLabel.text = @"详细地址";
        }
        
        
        
    }else{
        
        
        //        cell.textLabel.text = @"删除当前地址";
        
        
        if (_callbackClick) {
            _callbackClick(YES);
            
        }
        
        [self.navigationController popViewControllerAnimated:YES];
        
        
        
        
        
        
        
    }
    
    
}


@end
